using System;
using System.Diagnostics;

namespace OpenRay
{
	public class Hit : IComparable
	{
		public float Distance {
			get;
			private set;
		}
		
		private Primitive primitive;
		private Ray modelRay;
		private float[] interpolationData;
		
		public Hit ( Primitive o, Ray r, float d )
		{
			primitive = o.Copy();
			modelRay = new Ray( r );
			Distance = d;
			interpolationData = null;
		}
		
		public Hit ( Primitive o, Ray r, float d, float[] interp )
		{
			primitive = o.Copy();
			modelRay = new Ray( r );
			Distance = d;
			
			Debug.Assert( interp.Length >= 3, "TriangleHit: Interpolation data array is too short" );
			Array.Copy( interp, interpolationData, 3 );
		}
		
		public int CompareTo(Object obj)
		{
			if ( obj == null )
				return -1;
			
			Hit temp = obj as Hit;
			if (temp != null )
				return Distance.CompareTo( temp.Distance );
			else
				throw new ArgumentException( "Object is not a Hit" );
		}
		
		public Color GetColor(Ray worldRay)
		{
			return primitive.Shade(worldRay, modelRay, Distance, interpolationData);
		}
	}
}

